#!/usr/bin/env python

import os, sys
from address_book_class import AddressBook

#{{{def _create_dataset ( start_point, lines, rows, tot_rows ):
def _create_dataset ( start_point, lines, rows, tot_rows ):
	res = {}

	count = 0
	for v in rows:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v
		count += 1

	res [ 'rows' ] = tot_rows
	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = lines

	return res
#}}}
#{{{ PROFILE
#{{{def del_profile ( liwe, uids = "" ):
def del_profile ( liwe, uids = "", ids = "" ):
	"""
	:INPUT:
		-ids: SAFE_DB_STRING
		-uids: SAFE_DB_STRING
	:PERMS:
		-delete With this permission you can delete user profile
	       	- admin:               With this permission you can create/modify
	"""
	
	if not uids : uids = ids

	ab = AddressBook ( liwe )
	resp =  ab.del_profile ( uids =  uids ) 
	
#}}}
#{{{def list_profile ( liwe, name = '', lastname = '',level = 0, tax_code = '', vat_code = '', quant = 0,_X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
def list_profile ( liwe, uid = 0, name = '', lastname = '',level = 0, email = '', addr1 = '', prov = '', city = '', country = '', zip_code = '', 
					tax_code = '', vat_code = '', id_categ = 0, company = -1, individual = -1, quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
	
	"""
	:INPUT:
		- uid:			SAFE_NUMBER	
		- name:			SAFE_DB_STRING	
		- lastname:		SAFE_DB_STRING	
		- level:		SAFE_NUMBER	
		- email:		SAFE_DB_STRING
		- addr1:		SAFE_DB_STRING
		- prov:			SAFE_DB_STRING
		- city:			SAFE_DB_STRING
		- country:		SAFE_DB_STRING
		- zip_code:		SAFE_DB_STRING	
		- vat_code:		SAFE_DB_STRING	
		- tax_code:		SAFE_DB_STRING	
		- id_categ:		SAFE_NUMBER
		- company:		SAFE_NUMBER
		- individual:	SAFE_NUMBER
		- id_categ:	SAFE_NUMBER
		- quant:	SAFE_NUMBER
		- _X_LINES:	SAFE_NUMBER
		- _X_START_POINT: SAFE_NUMBER
		- _X_PAGE: SAFE_NUMBER
	:PERMS:
		- admin 
	"""

	use_dataset = 0

	if _X_LINES != -1: 
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	ab = AddressBook ( liwe )
	resp = ab.list_profile ( uid = uid, name = name, lastname = lastname, level = level, email = email,
				addr1 = addr1, prov = prov, city = city, country =country, zip_code = zip_code, id_categ = id_categ,
				tax_code = tax_code, vat_code = vat_code, company = company, individual = individual,
				quant = quant, start_point = start_point, do_count = use_dataset )


	rows = resp [ 'rows' ]
	tot_rows =  resp [ 'tot_rows' ]

	if not use_dataset: 
		return { 'profile': rows }

	return _create_dataset ( start_point, quant, rows, tot_rows )
#}}}
#}}}
#{{{ ADDRESS
#{{{def del_address ( liwe, ids = 0 ):
def del_address ( liwe, ids = 0 ):
	"""
	:INPUT:
		- ids:	SAFE_DB_STRING
	:PERMS:
		- delete With this permission you can delete user profile
	       	- admin:               With this permission you can create/modify
	"""
	ab = AddressBook ( liwe )
	resp =  ab.del_address ( ids = ids )
#}}}
#}}}
#{{{ KIND
#{{{def set_kind ( liwe, id_kind, descr = '', owner = 0 ):
def set_kind ( liwe, id_kind = 0, descr = '', owner = 0 ):
	"""
	:INPUT:
		- id_kind:	SAFE_NUMBER
		- descr:	SAFE_DB_STRING
		- owner:	SAFE_NUMBER
	:PERMS:
                - create:               With this permission you can create/modify
	       	- admin:               With this permission you can create/modify
	"""

        ab = AddressBook ( liwe )
	resp =  ab.set_kind ( id_kind = id_kind, descr = descr, owner =  owner )
	
	return { 'kind' : resp }	
#}}}
#{{{def get_kind ( liwe, id_kind ):
def get_kind ( liwe, id_kind = 0 ):
	"""
	:INPUT:
		-id_kind:	SAFE_NUMBER
		-uid:		SAFE_NUMBER
	:PERMS:
	       	- admin:               With this permission you can create/modify
	"""
	ab = AddressBook ( liwe )
	resp = ab.get_kind ( id_kind = id_kind )
	return { 'kind' : resp }
#}}}
#{{{def del_kind ( liwe, ids ):
def del_kind ( liwe, ids = "" ):
	"""
	:INPUT:
		- ids:	SAFE_DB_STRING
	:PERMS:
		- delete With this permission you can delete user profile
	       	- admin:               With this permission you can create/modify
	"""
	ab = AddressBook ( liwe )
	resp =  ab.del_kind ( ids = ids )
#}}}
#}}}
#{{{CATEGORIES
#{{{def set_categ ( liwe, text, parent_id = 0, id_node = None, params = '' ):
def set_categ ( liwe, text, parent_id = 0, id_node = None ):
	"""
	:INPUT:
		- id_node:      SAFE_NUMBER
		- text:			SAFE_STRING
		- parent_id:	SAFE_NUMBER

	:PERMS:
       	- admin:               With this permission you can create/modify
	"""

	n = AddressBook ( liwe )
	return { 'id': n.set_categ ( text, parent_id, id_node ) }
#}}}
#{{{def del_categ ( liwe, id_categ ):
def del_categ ( liwe, id_categ ):
	"""
	:INPUT:	
		- id_categ: 	SAFE_STRING
	:PERMS:
		- create:		With this permission you can create/modify news
	"""
	n = AddressBook ( liwe )

	res = n.del_category ( id_categ )

	return { "res": res }
#}}}
#{{{def list_categs ( liwe, id_node = None ):
def list_categs ( liwe, id_node = None ):
	"""
	:INPUT:
		- id_news:              SAFE_NUMBER
		- id_node:              SAFE_NUMBER

	:PERMS:
       	- admin:               With this permission you can create/modify
	"""

	n = AddressBook ( liwe )

	rs = n.list_categs ( id_node )
	if not rs: rs = []

	return { 'categs': rs }
#}}}
#{{{def list_am_categories ( liwe,  _X_LINES = 10,_X_START_POINT = 0, _X_PAGE = 0 ):
def list_am_categories ( liwe,  _X_LINES = 10,_X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
	
	:xPERMS:
		- admin:
	"""

        if _X_START_POINT:
                start_point = _X_START_POINT
        else:
                start_point = _X_PAGE * _X_LINES

        limit = _X_LINES


	n = AddressBook ( liwe )
	res = n.list_am_categories ( start_point = start_point, limit = limit )

        rows = res [ 'rows' ]
        tot_rows = res [ 'tot_rows' ]

	return _create_dataset ( start_point, limit, rows, tot_rows )
#}}}
#{{{def list_am_sub_categories ( liwe, id_parent = 0,  _X_LINES = 10,_X_START_POINT = 0, _X_PAGE = 0 ):
def list_am_sub_categories ( liwe, id_parent = 0,  _X_LINES = 10,_X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- id_parent:		SAFE_NUMBER
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
	
	:xPERMS:
		- admin:
	"""

        if _X_START_POINT:
                start_point = _X_START_POINT
        else:
                start_point = _X_PAGE * _X_LINES

        limit = _X_LINES


	n = AddressBook ( liwe )
	res = n.list_am_sub_categories ( id_parent = id_parent, start_point = start_point, limit = limit )

        rows = res [ 'rows' ]
        tot_rows = res [ 'tot_rows' ]

	return _create_dataset ( start_point, limit, rows, tot_rows )
#}}}
#}}}
#{{{def list_importable ( liwe ):
def list_importable ( liwe ):
	ab = AddressBook ( liwe )
	resp =  ab.list_importable ()
	return { 'user' : resp }
#}}}
